package com.dengyin2000.javaeye.ordering.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;

import javax.faces.model.SelectItem;

import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.framework.HibernateEntityHome;

import com.dengyin2000.javaeye.ordering.entity.Dish;
import com.dengyin2000.javaeye.ordering.entity.SnackShop;

@Name("snackShopAction")
public class SnackShopAction extends HibernateEntityHome<SnackShop> {

	private static final long serialVersionUID = -1143546741449335943L;
	
	private int selectOption;
	
	private Dish newDish;
	
	private List<SelectItem> selectOptions;
	
	@In
	FacesMessages facesMessages;

	public static Comparator<Dish> dishComparator = new Comparator<Dish>() {

		@Override
		public int compare(Dish o1, Dish o2) {
			return o1.getNo() - o2.getNo();
		}
		
	};

	@Override
	protected String getPersistenceContextName() {
		return "orderingDatabase";
	}
	
	@Factory("newSnackShop")
	public SnackShop buildNewSnackShop() {
		return getInstance();
	}
	
	public boolean isUnderUpdateDishStatus() {
		if (newDish == null)
			return false;
		return !(newDish.getId() == null);
	}
	
	public String addDishToSnackShop() {
		SnackShop shop = getInstance();
		switch (selectOption) {
		case 1:
			Set<Dish> monDishes = shop.getMonDishes();
			for (Dish dish : monDishes) {
				if (dish.getNo() == newDish.getNo()) {
					facesMessages.addToControl("no", "菜的序号已经存在请选择另外一个！");
					return "addDish";
				}
			}
			shop.getMonDishes().add(newDish);
			newDish.setMonId(shop.getId());
			break;
		case 2:
			Set<Dish> tusDishes = shop.getTuesDishes();
			for (Dish dish : tusDishes) {
				if (dish.getNo() == newDish.getNo()) {
					facesMessages.addToControl("no", "菜的序号已经存在请选择另外一个！");
					return "addDish";
				}
			}			
			shop.getTuesDishes().add(newDish);
			newDish.setTuesId(shop.getId());
			break;
		case 3:
			Set<Dish> wedDishes = shop.getWedDishes();
			for (Dish dish : wedDishes) {
				if (dish.getNo() == newDish.getNo()) {
					facesMessages.addToControl("no", "菜的序号已经存在请选择另外一个！");
					return "addDish";
				}
			}	
			shop.getWedDishes().add(newDish);
			newDish.setWedId(shop.getId());
			break;	
		case 4:
			Set<Dish> thuDishes = shop.getThuDishes();
			for (Dish dish : thuDishes) {
				if (dish.getNo() == newDish.getNo()) {
					facesMessages.addToControl("no", "菜的序号已经存在请选择另外一个！");
					return "addDish";
				}
			}				
			shop.getThuDishes().add(newDish);
			newDish.setThuId(shop.getId());
			break;	
		case 5:
			Set<Dish> friDishes = shop.getFriDishes();
			for (Dish dish : friDishes) {
				if (dish.getNo() == newDish.getNo()) {
					facesMessages.addToControl("no", "菜的序号已经存在请选择另外一个！");
					return "addDish";
				}
			}
			shop.getFriDishes().add(newDish);
			newDish.setFriId(shop.getId());
			break;	
		case 6:
			Set<Dish> satDishes = shop.getSatDishes();
			for (Dish dish : satDishes) {
				if (dish.getNo() == newDish.getNo()) {
					facesMessages.addToControl("no", "菜的序号已经存在请选择另外一个！");
					return "addDish";
				}
			}			
			shop.getSatDishes().add(newDish);
			newDish.setSatId(shop.getId());
			break;
		case 7:
			Set<Dish> sunDishes = shop.getSunDishes();
			for (Dish dish : sunDishes) {
				if (dish.getNo() == newDish.getNo()) {
					facesMessages.addToControl("no", "菜的序号已经存在请选择另外一个！");
					return "addDish";
				}
			}				
			shop.getSunDishes().add(newDish);
			newDish.setSunId(shop.getId());
			break;				
		default:
			break;
		}
		getSession().update(shop);
		facesMessages.add("成功添加！");
		return "addDish";
	}

	public int getSelectOption() {
		return selectOption;
	}

	public void setSelectOption(int selectOption) {
		this.selectOption = selectOption;
	}

	public List<SelectItem> getSelectOptions() {
		if (selectOptions == null) {
			selectOptions = new ArrayList<SelectItem>();
			selectOptions.add(new SelectItem(1, "星期一"));
			selectOptions.add(new SelectItem(2, "星期二"));
			selectOptions.add(new SelectItem(3, "星期三"));
			selectOptions.add(new SelectItem(4, "星期四"));
			selectOptions.add(new SelectItem(5, "星期五"));
			selectOptions.add(new SelectItem(6, "星期六"));
			selectOptions.add(new SelectItem(7, "星期七"));
		}
		return selectOptions;
	}

	public void setSelectOptions(List<SelectItem> selectOptions) {
		this.selectOptions = selectOptions;
	}

	public void selectDish(Dish dish) {
		newDish = dish;
	}

	public Dish getNewDish() {
		if (newDish == null)
			newDish = new Dish();
		return newDish;
	}

	public void setNewDish(Dish newDish) {

		this.newDish = newDish;
	}

}
